﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
#if !NETCOREAPP
using System.Web;
using System.Web.Mvc;
using System.Web.WebPages;
    using System.Web.Mvc.Html;

#endif

#if NETCOREAPP
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.Rendering;
using Kugar.Core.Web;
#endif
using Kugar.Core.ExtMethod;
using Kugar.MVC.MobileUI.WeUI.Helpers;

namespace Kugar.MVC.MobileUI.WeUI.PageControls.PageCommons
{
    public class PageCommon_ListActionBar: ContainerBase
    {
        public PageCommon_ListActionBar(ViewContext viewContext) : base(viewContext)
        {
        }

        public ListActionItem_Radio AddRadion()
        {
            return new ListActionItem_Radio(ViewContext);
        }

        public ListActionItem_Checkbox AddCheckbox()
        {
            return new ListActionItem_Checkbox(ViewContext);
        }

        public ListActionItem_SwitchButton AddSwitchButton()
        {
            return new ListActionItem_SwitchButton(ViewContext);
        }

        public override void Dispose()
        {
            ViewContext.Writer.WriteLine("</div> <!-- end weui-flex -->");

            ViewContext.Writer.WriteLine("</div> <!-- end listbar -->");
        }

        internal override ContainerBase RenderInternal()
        {
            ViewContext.Writer.WriteLine("<div class=\"listbar\">");
            ViewContext.Writer.WriteLine("<div class=\"weui-flex\">");

            return this;
        }

        public class ListActionItem_Radio : ControlBase
        {
            private List<SelectorItem> _lst = new List<SelectorItem>(5);
            private string _radioValue = "";
            private string _radioName = "";
            private string _label = "";
            private string _id = "";
            private bool _onchangeSubmit = false;
            private int _displayPerRow = 4;

            public ListActionItem_Radio(ViewContext viewContext) : base(viewContext)
            {
                this.Css("weui-flex weui-querybox-radio");

            }

            public ListActionItem_Radio FirstItem(string title, string value)
            {
                _lst.Insert(0, new SelectorItem(title, value));

                return this;
            }

            public ListActionItem_Radio Options(IEnumerable<SelectorItem> opts)
            {
                _lst.AddRange(opts);

                return this;
            }

            /// <summary>
            /// 单选框的选项,使用一个数据源以及标题获取函数和值获取函数
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="source"></param>
            /// <param name="titleFunc"></param>
            /// <param name="valueFunc"></param>
            /// <returns></returns>
            public ListActionItem_Radio Options<T>(IEnumerable<T> source, Func<T, string> titleFunc,
                Func<T, string> valueFunc)
            {
                _lst.Clear();

                _lst.AddRange(OptionHelper.Convert(source, titleFunc, valueFunc));

                return this;
            }

            /// <summary>
            /// 单选框的选项,使用一个数据源以及标题获取函数和值获取函数
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="source"></param>
            /// <param name="titleFunc"></param>
            /// <param name="valueFunc"></param>
            /// <returns></returns>
            public ListActionItem_Radio Options<T>(IEnumerable<T> source, Func<T, HelperResult> titleFunc,
                Func<T, HelperResult> valueFunc)
            {
                _lst.Clear();

                _lst.AddRange(OptionHelper.Convert(source, (data) => titleFunc(data).ToHtmlString(), (data) => valueFunc(data).ToHtmlString()));

                return this;
            }
            
            /// <summary>
            /// 单选框的选项,用于enum的显示
            /// </summary>
            /// <param name="enumType"></param>
            /// <returns></returns>
            public ListActionItem_Radio Options(Type enumType)
            {
                _lst.Clear();

                _lst.AddRange(OptionHelper.Convert(enumType));

                return this;
            }

            /// <summary>
            /// 单选框的name
            /// </summary>
            /// <param name="name"></param>
            /// <returns></returns>
            public ListActionItem_Radio Name(string name)
            {
                _radioName = name;

                return this;
            }

            /// <summary>
            /// 选择的值
            /// </summary>
            /// <param name="value"></param>
            /// <returns></returns>
            public ListActionItem_Radio Value(string value)
            {
                _radioValue = value;

                return this;
            }

            /// <summary>
            /// 选择的值
            /// </summary>
            /// <param name="value"></param>
            /// <returns></returns>
            public ListActionItem_Radio Value(int value)
            {
                _radioValue = value.ToString();

                return this;
            }

            /// <summary>
            /// 标签
            /// </summary>
            /// <param name="label"></param>
            /// <returns></returns>
            public ListActionItem_Radio Label(string label)
            {
                _label = label;

                return this;
            }
            
            /// <summary>
            /// 当修改选项值时,是否自动提交
            /// </summary>
            /// <param name="autoSubmit"></param>
            /// <returns></returns>
            public ListActionItem_Radio AutoSubmitOnChange(bool autoSubmit = true)
            {
                _onchangeSubmit = autoSubmit;

                return this;
            }

            public override MvcHtmlString Render()
            {
                ViewContext.Writer.WriteLine($"<div {_attrs} data-display=\"{_displayPerRow}\" data-onchangesubmit=\"{_onchangeSubmit.ToStringEx().ToLower()}\">");
                ViewContext.Writer.WriteLine("<div class=\"weui-flex__item weui-flex-row\">");

                ViewContext.Writer.WriteLine($"<div class=\"weui-cells__title\">");

                ViewContext.Writer.WriteLine($"<span>{_label}</span>");

                if (_lst.Count > _displayPerRow * 2) //如果超出2行的数量,则显示一个下拉按钮
                {
                    ViewContext.Writer.WriteLine("<span class=\"btnShowAll\">全部 &and;</span></div>");
                }

                ViewContext.Writer.WriteLine("</div> <!-- end cells__title -->");

                ViewContext.Writer.WriteLine("<div class=\"weui-cells weui-cells__body\">");

                if (string.IsNullOrWhiteSpace(_radioValue))
                {
                    _radioValue = _lst[0].Value;
                }

                for (int i = 0; i < _lst.Count; i += _displayPerRow)
                {
                    ViewContext.Writer.WriteLine("<div class=\"weui-flex weui-querybox-block\">");

                    for (int j = 0; j < 3; j++)
                    {
                        var item = _lst[i + j];

                        ViewContext.Writer.WriteLine($"<div class=\"weui-flex__item weui-querybox-item\"><input type=\"radio\" {(_radioValue == item.Value).If("checked=\"checked\"")} name=\"{_radioName}\" value=\"{item.Value}\" /><p>{item.Title}</p></div>");
                    }

                    ViewContext.Writer.WriteLine("</div> <!-- end querybox-block -->");
                }

                ViewContext.Writer.WriteLine("</div> <!-- end cells__body -->");
                ViewContext.Writer.WriteLine("</div> <!-- end flex-row -->");
                ViewContext.Writer.WriteLine("</div> <!-- end querybox-radio -->");

                return MvcHtmlString.Empty;
            }
        }

        public class ListActionItem_Checkbox : ControlBase
        {
            private List<SelectorItem> _lst = new List<SelectorItem>(5);
            private string[] _checkValue = null;
            private string _checkboxName = "";
            private string _label = "";
            private string _id = "";
            private bool _onchangeSubmit = false;
            private int _displayPerRow = 4;

            public ListActionItem_Checkbox(ViewContext viewContext) : base(viewContext)
            {
                this.Css("weui-flex weui-querybox-check");

            }

            public ListActionItem_Checkbox FirstItem(string title, string value)
            {
                _lst.Insert(0, new SelectorItem(title, value));

                return this;
            }

            public ListActionItem_Checkbox Options(IEnumerable<SelectorItem> opts)
            {
                _lst.AddRange(opts);

                return this;
            }

            /// <summary>
            /// 单选框的选项,使用一个数据源以及标题获取函数和值获取函数
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="source"></param>
            /// <param name="titleFunc"></param>
            /// <param name="valueFunc"></param>
            /// <returns></returns>
            public ListActionItem_Checkbox Options<T>(IEnumerable<T> source, Func<T, string> titleFunc,
                Func<T, string> valueFunc)
            {
                _lst.Clear();

                _lst.AddRange(OptionHelper.Convert(source, titleFunc, valueFunc));

                return this;
            }

            /// <summary>
            /// 单选框的选项,使用一个数据源以及标题获取函数和值获取函数
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="source"></param>
            /// <param name="titleFunc"></param>
            /// <param name="valueFunc"></param>
            /// <returns></returns>
            public ListActionItem_Checkbox Options<T>(IEnumerable<T> source, Func<T, HelperResult> titleFunc,
                Func<T, HelperResult> valueFunc)
            {
                _lst.Clear();

                _lst.AddRange(OptionHelper.Convert(source, (data) => titleFunc(data).ToHtmlString(), (data) => valueFunc(data).ToHtmlString()));

                return this;
            }


            /// <summary>
            /// 单选框的选项,用于enum的显示
            /// </summary>
            /// <param name="enumType"></param>
            /// <returns></returns>
            public ListActionItem_Checkbox Options(Type enumType)
            {
                _lst.Clear();

                _lst.AddRange(OptionHelper.Convert(enumType));

                return this;
            }

            /// <summary>
            /// 单选框的name
            /// </summary>
            /// <param name="name"></param>
            /// <returns></returns>
            public ListActionItem_Checkbox Name(string name)
            {
                _checkboxName = name;

                return this;
            }

            /// <summary>
            /// 选择的值
            /// </summary>
            /// <param name="value"></param>
            /// <returns></returns>
            public ListActionItem_Checkbox Value(IEnumerable<string> value)
            {
                _checkValue = value.ToArrayEx();

                return this;
            }

            /// <summary>
            /// 选择的值
            /// </summary>
            /// <param name="value"></param>
            /// <returns></returns>
            public ListActionItem_Checkbox Value(int[] value)
            {
                _checkValue = value.Select(x => x.ToString()).ToArrayEx();

                return this;
            }

            /// <summary>
            /// 标签
            /// </summary>
            /// <param name="label"></param>
            /// <returns></returns>
            public ListActionItem_Checkbox Label(string label)
            {
                _label = label;

                return this;
            }

            /// <summary>
            /// 当修改选项值时,是否自动提交
            /// </summary>
            /// <param name="autoSubmit"></param>
            /// <returns></returns>
            public ListActionItem_Checkbox AutoSubmitOnChange(bool autoSubmit = true)
            {
                _onchangeSubmit = autoSubmit;

                return this;
            }

            public override MvcHtmlString Render()
            {
                ViewContext.Writer.WriteLine($"<div {_attrs} data-display=\"{_displayPerRow}\" data-onchangesubmit=\"{_onchangeSubmit.ToStringEx().ToLower()}\">");
                ViewContext.Writer.WriteLine("<div class=\"weui-flex__item weui-flex-row\">");

                ViewContext.Writer.WriteLine($"<div class=\"weui-cells__title\">");

                ViewContext.Writer.WriteLine($"<span>{_label}</span>");

                if (_lst.Count > _displayPerRow * 2) //如果超出2行的数量,则显示一个下拉按钮
                {
                    ViewContext.Writer.WriteLine("<span class=\"btnShowAll\">全部 &and;</span></div>");
                }

                ViewContext.Writer.WriteLine("</div> <!-- end cells__title -->");

                ViewContext.Writer.WriteLine("<div class=\"weui-cells weui-cells__body\">");

                //if (string.IsNullOrWhiteSpace(_radioValue))
                //{
                //    _radioValue = _lst[0].Value;
                //}

                for (int i = 0; i < _lst.Count; i += _displayPerRow)
                {
                    ViewContext.Writer.WriteLine("<div class=\"weui-flex weui-querybox-block\">");

                    for (int j = 0; j < 3; j++)
                    {
                        var item = _lst[i + j];

                        ViewContext.Writer.WriteLine($"<div class=\"weui-flex__item weui-querybox-item\"><input type=\"checkbox\" {(_checkValue.Contains(item.Value)).If("checked=\"checked\"")} name=\"{_checkboxName}\" value=\"{item.Value}\" /><p>{item.Title}</p></div>");
                    }

                    ViewContext.Writer.WriteLine("</div> <!-- end querybox-block -->");
                }

                ViewContext.Writer.WriteLine("</div> <!-- end cells__body -->");
                ViewContext.Writer.WriteLine("</div> <!-- end flex-row -->");
                ViewContext.Writer.WriteLine("</div> <!-- end querybox-radio -->");

                return MvcHtmlString.Empty;
            }
        }

        public class ListActionItem_SwitchButton: ControlBase
        {
            private SwitchType _type = SwitchType.Radio;
            private bool _noborder = false;
            private bool _nobackgroudcolor = false;
            private string _name = "";
            private string _label = "";
            private string _value = "";
            private bool _isChecked = false;
            private bool _autosubmit = false;

            public ListActionItem_SwitchButton(ViewContext viewContext) : base(viewContext)
            {
                this.Css("weui-flex__item listbar-item-switch");
            }

            public ListActionItem_SwitchButton Type(SwitchType type= SwitchType.Radio)
            {
                _type = type;

                return this;
            }

            public ListActionItem_SwitchButton NoBorder(bool noborder = true)
            {
                _noborder = noborder;

                return this;
            }

            public ListActionItem_SwitchButton NoBackgroudColor(bool nocolor = true)
            {
                _nobackgroudcolor = nocolor;

                return this;
            }

            public ListActionItem_SwitchButton Name(string name)
            {
                _name = name;
                return this;
            }

            public ListActionItem_SwitchButton Label(string label)
            {
                _label = label;

                return this;
            }

            public ListActionItem_SwitchButton Value(string value)
            {
                _value = value;

                return this;
            }

            public ListActionItem_SwitchButton Checked(bool isChecked = true)
            {
                _isChecked = isChecked;

                return this;
            }

            public ListActionItem_SwitchButton AutoSubmitOnChange(bool autoSubmit = true)
            {
                _autosubmit = autoSubmit;

                return this;
            }
            
            public override MvcHtmlString Render()
            {
        
                this.Data("autosubmit", _autosubmit.ToStringEx());
                
                ViewContext.Writer.WriteLine($"<div {_attrs}>");

                ViewContext.Writer.WriteLine($"<div class=\"listbar-item-btn listbar-item-switch-btn {_nobackgroudcolor.If("listbar-item-btn-nobackground")} {_noborder.If("listbar-item-btn-noborder")}\" {_isChecked.If("listbar-item-switch-btn-checked")} >");
                
                ViewContext.Writer.WriteLine($"<span class=\"title\">{_label}</span><input style=\"display:none;\" type=\"{(_type== SwitchType.Radio?"radio":"checkbox")}\" {_isChecked.If("checked=\"checked\"")} name=\"{_name}\" value=\"{_value}\" />");

                ViewContext.Writer.WriteLine("</div> <!-- end  listbar-item-btn-->");
                ViewContext.Writer.WriteLine("</div> <!-- end listbar-item-switch -->");

                return MvcHtmlString.Empty;
            }
        }

        public enum SwitchType
        {
            Radio=0,

            Checkbox=1
        }
    }
}